package se137.hrbust.hlgshopping137.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import se137.hrbust.hlgshopping137.entity.PageResult137;
import se137.hrbust.hlgshopping137.entity.Product137;
import se137.hrbust.hlgshopping137.service.Product137Service;
import se137.hrbust.hlgshopping137.utils.Result137;
import se137.hrbust.hlgshopping137.utils.ResultEnum137;
import se137.hrbust.hlgshopping137.utils.SQLCommService137;

import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController137 {
    @Autowired
    Product137Service product137Service;
    @Autowired
    SQLCommService137 commService137;
    @RequestMapping("/get/{id}")
    public Product137 getProductById137(@PathVariable int id){
        return product137Service.getById(id);
    }

    @RequestMapping("/page")
    public PageResult137<Product137> getProductPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "2") long size,
            @RequestParam(required = false) String pName
    ){
        Page<Product137> page = new Page<>(current, size);
       LambdaQueryWrapper<Product137> query= Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(pName)){
            query.like(Product137::getPname137, pName);
        }
        Page<Product137> productPage = product137Service.page(page, query);
        return PageResult137.convert(productPage);
    }
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable int id){
        product137Service.removeById(id);
    }
    //批零删除
    @RequestMapping("/dels")
    public void deleteProducts(int[] ids){
       for(int id:ids)
           product137Service.removeById(id);
    }
    @GetMapping("/up/{id}")
    public void upProduct(@PathVariable int id){
    String sql=String.format("update tb_product137 set FPListSate=1 where FPID=%d",id);
            commService137.updateSQL(sql);
            System.out.println(sql);
        }
        @GetMapping("/down/{id}")
    public void downProduct(@PathVariable int id){
        String sql=String.format("update tb_product137 set FPListSate=0 where FPID=%d",id);
        commService137.updateSQL(sql);
        System.out.println(sql);
        }
    @GetMapping("/detail/{id}")
    public Result137 getProductDetail(@PathVariable Long id) {
        Map<String, Object> productDetail = product137Service.getProductDetail(id);
        if (productDetail == null) {
         return new Result137().setCode(ResultEnum137.NOT_FOUND);

        }
         return new Result137().ok(productDetail);
    }


    }

